$TITLE ('RTX-51 ISR-SEND-FUNCTIONS')
$SYMBOLS
$NOXREF
$NOCOND
$NOMOD51
$NORB
$PAGELENGTH(80) PAGEWIDTH(110)
;************************************************************************
;*									*
;*    R T X - 5 1  :   RTX-51 ISR-SEND-FUNCTIONS                        *
;*							     		*
;*----------------------------------------------------------------------*
;*                                                                      *
;*    Filename     :   RTXISEND.A51                                     *
;*    Language     :   Keil A-51                                        *
;*    Dev. system  :   Keil uVision2                                    *
;*    Targetsystem :   Any system based upon 8051 up                    *
;*                                                                      *
;*    Purpose      :   Implements the RTX-51 ISR-Functions              *
;*								     	*
;*----------------------------------------------------------------------*
;* Rev. |  Released   | Programmer    | Comments                        *
;*----------------------------------------------------------------------*
;* 0.1  |  19.6.1991  | Th. Fischler  | First Version for RTX-51 V4     *
;* 5.00 | 20.10.1994  | E. Glatz      | Release 5.00                    *
;* 7.00 | 13.11.2001  | T. Mueller    | Release 7.00                    *
;************************************************************************
;*    Copyright 1991 .. 2001 METTLER  &  FUCHS  AG,  CH-8953 Dietikon   *
;************************************************************************

$INCLUDE (RTXDEBUG.INC)
$INCLUDE (RTXIMASK.INC)

;*----------------------------------------------------------------------*
;*	I M P O R T S
;*----------------------------------------------------------------------*

$INCLUDE (RTXCONST.INC)
$INCLUDE (RTXEXTRN.INC)

;        FROM RTXCONF
EXTRN    BIT   (?RTX_TFLAG)


;*----------------------------------------------------------------------*
;*	E X P O R T S
;*----------------------------------------------------------------------*

PUBLIC   _isr_send_signal
PUBLIC   _isr_send_message


;*----------------------------------------------------------------------*
;*	P R O C E D U R E S
;*----------------------------------------------------------------------*

;*----------------------------------------------------------------------*
;*      I S R _ S E N D _ S I G N A L
;*----------------------------------------------------------------------*
;*      System call for sending a signal from an ISR to a task.
;*----------------------------------------------------------------------*
;*      Input:   - R7: destination task number
;*
;*      Output:  - Returns execution status in R7 (OK, NOT_OK)
;*
;*----------------------------------------------------------------------*

?RTX?_isr_send_signal?RTXISEND   SEGMENT CODE
                                 RSEG    ?RTX?_isr_send_signal?RTXISEND

_isr_send_signal:
               DBG_SYS_ENTRY
               ; Check task-number
               MOV   A, R7
               SETB  C
               SUBB  A, #?RTX_MAXTASKN
               JC    EVAL_INTERNAL_NUM
               ; Number is greater than the maximum used task-number
               MOV   R7, #NOT_OK
               DBG_SYS_EXIT
               RET

EVAL_INTERNAL_NUM:
               ; Evaluate the internal Task-Number
               MOV   DPH, #HIGH(?RTX_USER_NUM_TABLE)
               MOV   A, #LOW(?RTX_USER_NUM_TABLE)
               ADD   A, R7
               MOV   DPL, A
               ; DPTR -> user_num_table[user_num]
               MOVX  A, @DPTR
               ; Check if valid task
               CJNE  A, #NO_TASK, TASK_FOUND
               MOV   R7, #NOT_OK
               DBG_SYS_EXIT
               RET

TASK_FOUND:    ; Check if free place in LIFO
               GLOBAL_INT_DISABLE
               MOV   A, ?RTX_ACT_LIFO_EL
               CLR   C
               SUBB  A, #ISR_LIFO_SIZE-1
               JC    LIFO_FREE1
               MOV   R7, #NOT_OK
               GLOBAL_INT_ENABLE
               DBG_SYS_EXIT
               RET                              ; no free place in LIFO

LIFO_FREE1:    MOV   A, ?RTX_ACT_LIFO_EL
               ADD   A, #LOW(?RTX_ISR_LIFO)
               MOV   DPL, A
               MOV   DPH, #HIGH(?RTX_ISR_LIFO)
               ; Setup LIFO-entry
               MOV   A, #ISR_SEND_SIG_CMD
               MOVX  @DPTR, A                   ; store Command
               INC   DPL
               INC   DPL
               MOV   A, R7
               MOVX  @DPTR, A                   ; store task-number
               MOV   A, ?RTX_ACT_LIFO_EL
               ADD   A, #3
               MOV   ?RTX_ACT_LIFO_EL, A        ; ?RTX_ACT_LIFO_EL incremented


               ; Check if System-Clock-Handler is active
COMMON:        JB    ?RTX_SYSCLK_ISR, CLK_ACTIVE
               SETB  ?RTX_TFLAG                 ; activate overflow-INT
CLK_ACTIVE:    MOV   R7, #OK
               GLOBAL_INT_ENABLE
               DBG_SYS_EXIT
               RET



;*----------------------------------------------------------------------*
;*      I S R _ S E N D _ M E S S A G E
;*----------------------------------------------------------------------*
;*      System call for sending a message from an ISR to a task.
;*----------------------------------------------------------------------*
;*      Input:   - R7: Mailbox-Number
;*               - R4: HIGH(message)
;*               - R5: LOW(message)
;*
;*      Output:  - Returns execution status in R7 (OK, NOT_OK)
;*
;*----------------------------------------------------------------------*

?RTX?_isr_send_message?RTXISEND   SEGMENT CODE
                                  RSEG    ?RTX?_isr_send_message?RTXISEND

_isr_send_message:
               DBG_SYS_ENTRY
               ; Check the mailbox-parameter
               MOV   A, R7
               SETB  C
               SUBB  A, #7H
               JC    MBX_OK
               ; Mailbox-Nbr is not ok
               MOV   R7, #NOT_OK
               DBG_SYS_EXIT
               RET

MBX_OK:        ; Check if free place in LIFO
               GLOBAL_INT_DISABLE
               MOV   A, ?RTX_ACT_LIFO_EL
               CLR   C
               SUBB  A, #ISR_LIFO_SIZE-1
               JC    LIFO_FREE2
               MOV   R7, #NOT_OK
               GLOBAL_INT_ENABLE
               DBG_SYS_EXIT
               RET                              ; no free place in LIFO

LIFO_FREE2:    MOV   A, ?RTX_ACT_LIFO_EL
               ADD   A, #LOW(?RTX_ISR_LIFO)
               MOV   DPL, A
               MOV   DPH, #HIGH(?RTX_ISR_LIFO)
               ; Setup LIFO-entry
               MOV   A, R7
               MOVX  @DPTR, A                   ; store Command / Mbx-Nbr
               INC   DPL
               MOV   A, R4
               MOVX  @DPTR, A                   ; store HIGH(message)
               INC   DPL
               MOV   A, R5
               MOVX  @DPTR, A                   ; store LOW(message)
               MOV   A, ?RTX_ACT_LIFO_EL
               ADD   A, #3
               MOV   ?RTX_ACT_LIFO_EL, A        ; ?RTX_ACT_LIFO_EL incremented

               JMP   COMMON                     ; Use code from
                                                ; isr_send_signal


;
;   END OF MODULE
;
	END

